﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace FGP_Kpaint
{
    class Gallery
    {
        MainWindow main;
        //The paths of the picture files.
        private readonly string[] picturePaths = CreatePicturePaths();

        //The index of the current image.
        private int indexField = 1;
        public BitmapImage PreviousPicture { get; private set; }

        // Gets the current image to be displayed.
        public BitmapImage Picture { get; private set; }

        /// Gets teh next image displayed.
        public BitmapImage NextPicture { get; private set; }

        public Gallery(MainWindow mains)
        {

            this.main = mains;
           // main.textBlock1.Text = "1";
           this.PreviousPicture = this.LoadPicture(this.Index - 1);
            this.Picture = this.LoadPicture(this.Index);
            this.NextPicture = this.LoadPicture(this.Index + 1);

            /*main.galleryImage.Source = this.Picture;
            main.previousImage.Source = this.PreviousPicture;
            main.nextImage.Source = this.NextPicture;*/

           // main.image1.Source = this.Picture;
        }



        public BitmapImage getNextImage()
        {
            Index += 1;
            // Setup corresponding picture if pictures are available.
            this.PreviousPicture = this.Picture;
            this.Picture = this.NextPicture;
            this.NextPicture = LoadPicture(Index + 1);

            main.textBlock1.Text = Index.ToString();
            main.galleryImage.Source = this.Picture;
            main.previousImage.Source = this.PreviousPicture;
            main.nextImage.Source = this.NextPicture;
            return this.Picture;

        }

        public BitmapImage getPreviousImage()
        {
            Index -= 1;
            // Setup corresponding picture if pictures are available.
            this.NextPicture = this.Picture;
            this.Picture = this.PreviousPicture;
            this.PreviousPicture = LoadPicture(Index - 1);


            main.textBlock1.Text = Index.ToString();
            main.galleryImage.Source = this.Picture;
            main.previousImage.Source = this.PreviousPicture;
            main.nextImage.Source = this.NextPicture;
            return this.Picture;

        }


        //Get list of files to display as pictures.
        //Paths to pictures.
        private static string[] CreatePicturePaths()
        {
            
            var list = new List<string>();

            DirectoryInfo myDirectory = new DirectoryInfo(Environment.CurrentDirectory);
            string parentDirectory = myDirectory.Parent.Parent.FullName;
            parentDirectory += Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"\images");

            var commonPicturesPath = parentDirectory;

            list.AddRange(Directory.GetFiles(commonPicturesPath, "*.jpg", SearchOption.AllDirectories));
            if (list.Count == 0)
            {
                list.AddRange(Directory.GetFiles(commonPicturesPath, "*.png", SearchOption.AllDirectories));
            }

            if (list.Count == 0)
            {
                var myPicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                list.AddRange(Directory.GetFiles(myPicturesPath, "*.jpg", SearchOption.AllDirectories));
                if (list.Count == 0)
                {
                    list.AddRange(Directory.GetFiles(commonPicturesPath, "*.png", SearchOption.AllDirectories));
                }
            }

            return list.ToArray();
        }

        //Load the picture with the given index.
        //eturns Corresponding image.
        private BitmapImage LoadPicture(int index)
        {
            BitmapImage value;

            if (this.picturePaths.Length != 0)
            {
                var actualIndex = index % this.picturePaths.Length;
                if (actualIndex < 0)
                {
                    actualIndex += this.picturePaths.Length;
                }

                Debug.Assert(0 <= actualIndex, "Index used will be non-negative");
                Debug.Assert(actualIndex < this.picturePaths.Length, "Index is within bounds of path array");

                try
                {
                    value = new BitmapImage(new Uri(this.picturePaths[actualIndex]));
                }
                catch (NotSupportedException)
                {
                    value = null;
                }
            }
            else
            {
                value = null;
            }

            return value;
        }

        public void stopImages() {
            main.galleryImage.Source = null;
            main.nextImage.Source = null;
            main.previousImage.Source = null;
        
        }

        public int Index
        {
            get
            {
                return this.indexField;
            }

            set
            {
                if (this.indexField != value)
                {
                    this.indexField = value;

                    // Notify world of change to Index and Picture.
                    /* if (this.PropertyChanged != null)
                     {
                         this.PropertyChanged(this, new PropertyChangedEventArgs("Index"));
                     }*/
                }
            }
        }

    }
}
